//
//  WordsCardView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/22.
//

#import "WordsCardView.h"
#import "WordSummaryListCell.h"
#import <AVFoundation/AVFoundation.h>

@interface WordsCardView()<
UITableViewDelegate,
UITableViewDataSource
///AVSpeechSynthesizerDelegate
>

@property(nonatomic)UIView *bgView;
@property(nonatomic,strong)UIButton *topErrorBtn;
@property(nonatomic,strong)UIButton *collectBtn;
@property(nonatomic,strong)UILabel *pronunciationLabel;//发音
@property(nonatomic,strong)UILabel *writeLabel;//书写
@property(nonatomic,strong)UILabel *toneLabel;//音调
@property(nonatomic,strong)UILabel *wordTipLabel;
@property(nonatomic,strong)UILabel *wordLabel;
@property(nonatomic,strong)UILabel *exampleTipLabel;
@property(nonatomic,strong)UILabel *exampleLabel;
@property(nonatomic,strong)UILabel *exampleTranslateLabel;

//小结
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIButton *beginBtn;
//@property(nonatomic,strong)AVSpeechSynthesizer *synthesizer;
@property(nonatomic,strong)WordSummaryListCell *currentCell;
@property(nonatomic,assign)NSInteger playIndex;


//测试
@property(nonatomic,strong)UILabel *questionLabel;
@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UIButton *correctBtn;
@property(nonatomic,strong)UIButton *errorBtn;
@property(nonatomic,strong)UIImageView *pronunciationImageView;

//测试选择翻牌后
@property(nonatomic,strong)UIButton *unknowBtn;
@property(nonatomic,strong)UIButton *nextBtn;




@end

@implementation WordsCardView

-(instancetype)initWithFrame:(CGRect)frame andCurrentAvailableWidth:(CGFloat)currentAvailableWidth andCurrentAvailableHeight:(CGFloat)currentAvailableHeight{
    self = [super initWithFrame:frame];
    if(self){
        self.currentAvailableWidth = currentAvailableWidth;
        self.currentAvailableHeight = currentAvailableHeight;
        [self createUI];
    }
    return self;
}


-(void)createUI{
    
    self.backgroundColor = kCardBgColor(self.width,self.height);
    self.layer.cornerRadius = 20;
    self.layer.shadowColor = UIColorHex_(0x000000,0.06).CGColor;
    self.layer.shadowOffset = CGSizeMake(0,2);
    self.layer.shadowOpacity = 1;
    self.layer.shadowRadius = 20;
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width,self.height)];
    self.bgView.backgroundColor = kCardBgColor(self.width,self.height);
    self.bgView.layer.cornerRadius = 20;
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
    
    //单词卡片
    self.topErrorBtn = [[UIButton alloc]initWithFrame:CGRectMake(8,6,48,30)];
    [self.topErrorBtn setTitle:@"报错" forState:UIControlStateNormal];
    [self.topErrorBtn setTitleColor:UIColorHex(0xB5CCC7) forState:UIControlStateNormal];
    self.topErrorBtn.titleLabel.font = JPUIFontPingFangSCRegular(14);
    [self.topErrorBtn addTarget:self action:@selector(topErrorBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.topErrorBtn];
    
    self.collectBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width-46,6,40,40)];
    [self.collectBtn setImage:[UIImage imageNamed:@"word_card_uncollect"] forState:UIControlStateNormal];
    [self.collectBtn setImage:[UIImage imageNamed:@"word_card_collect"] forState:UIControlStateSelected];
    self.collectBtn.ym_acceptEventInterval = 1.0;
    [self.collectBtn addTarget:self action:@selector(collectBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.collectBtn];
   
    CGFloat h120 = [DeviceUtils getAdaptedHeightForDesignHeight:120 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.pronunciationLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,h120, self.width-32,34)];
    self.pronunciationLabel.font = JPUIFontNotoSansJPMedium(24);
    self.pronunciationLabel.textColor = KFontBlackColor;
    self.pronunciationLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.pronunciationLabel];
    
    CGFloat left = (self.width-2)/2.0;
    self.writeLabel = [[UILabel alloc]initWithFrame:CGRectMake(left,CGRectGetMaxY(self.pronunciationLabel.frame),2,67)];
    self.writeLabel.font = JPUIFontNotoSansJPMedium(48);
    self.writeLabel.textColor = KFontBlackColor;
    self.writeLabel.textAlignment = NSTextAlignmentCenter;
    self.writeLabel.userInteractionEnabled = YES;
    [self addSubview:self.writeLabel];
    __weak typeof(self)weakSelf = self;
    UITapGestureRecognizer *writeTap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if(weakSelf.correctBtn.hidden == YES){
            if(weakSelf.model.audio&&![weakSelf.model.audio isEqualToString:@""]){
                //有单词音频
                [weakSelf playAudio:weakSelf.model.audio];
            }else{
                if((weakSelf.model.pronunciation)&&(![weakSelf.model.pronunciation isEqualToString:@""])){
                    if((weakSelf.model.writing)&&(weakSelf.model.isReadWriting)&&([weakSelf.model.isReadWriting isEqualToString:@"1"]||weakSelf.model.isReadWriting.integerValue == 1)){
                        [weakSelf speakJapaneseText:weakSelf.model.writing];
                    }else{
                        [weakSelf speakJapaneseText:weakSelf.model.pronunciation];
                    }
                }else{
                    if((weakSelf.model.writing)&&(![weakSelf.model.writing isEqualToString:@""])){
                        [weakSelf speakJapaneseText:weakSelf.model.writing];
                    }
                }
            }
        }
    }];
    [self.writeLabel addGestureRecognizer:writeTap];
     
    self.toneLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.writeLabel.frame)+10, CGRectGetMaxY(self.writeLabel.frame)-self.writeLabel.frame.size.height+6, 20,28)];
    self.toneLabel.font = JPUIFontNotoSansJPMedium(20);
    self.toneLabel.textColor = KFontBlackColor;
    self.toneLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.toneLabel];
    
    CGFloat h100 = [DeviceUtils getAdaptedHeightForDesignHeight:100 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.wordTipLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,CGRectGetMaxY(self.writeLabel.frame)+h100,46,22)];
    self.wordTipLabel.font = JPUIFontPingFangSCRegular(13);
    self.wordTipLabel.textColor = KThemeColor;
    self.wordTipLabel.textAlignment = NSTextAlignmentCenter;
    self.wordTipLabel.backgroundColor = UIColorHex(0xE8FAF5);
    self.wordTipLabel.layer.cornerRadius = 11;
    self.wordTipLabel.layer.masksToBounds = YES;
    [self addSubview:self.wordTipLabel];
    
    CGFloat h10 = [DeviceUtils getAdaptedHeightForDesignHeight:10 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.wordLabel = [[UILabel alloc]initWithFrame:CGRectMake(30,CGRectGetMaxY(self.wordTipLabel.frame)+h10,self.width-30-20,20)];
    self.wordLabel.numberOfLines = 0;
    self.wordLabel.font = JPUIFontPingFangSCRegular(14);
    self.wordLabel.textColor = KFontGrayColor;
    [self addSubview:self.wordLabel];
    
    CGFloat h25 = [DeviceUtils getAdaptedHeightForDesignHeight:25 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.exampleTipLabel = [[UILabel alloc]initWithFrame:CGRectMake(20,CGRectGetMaxY(self.wordLabel.frame)+h25,46,22)];
    self.exampleTipLabel.font = JPUIFontPingFangSCRegular(13);
    self.exampleTipLabel.textColor = KThemeColor;
    self.exampleTipLabel.text = @"例句";
    self.exampleTipLabel.textAlignment = NSTextAlignmentCenter;
    self.exampleTipLabel.backgroundColor = UIColorHex(0xE8FAF5);
    self.exampleTipLabel.layer.cornerRadius = 11;
    self.exampleTipLabel.layer.masksToBounds = YES;
    [self addSubview:self.exampleTipLabel];
    
    self.exampleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30,CGRectGetMaxY(self.exampleTipLabel.frame)+h10,self.width-30-20,20)];
    self.exampleLabel.numberOfLines = 0;
    self.exampleLabel.userInteractionEnabled = YES;
    self.exampleLabel.textColor = KFontGrayColor;
    self.exampleLabel.font = JPUIFontNotoSansJPRegular(14);
    [self addSubview:self.exampleLabel];
    UITapGestureRecognizer *exampleTap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if(weakSelf.model.example&&weakSelf.model.example.length > 0&&weakSelf.correctBtn.hidden == YES){
            [weakSelf speakJapaneseText:weakSelf.model.example];
        }
    }];
    [self.exampleLabel addGestureRecognizer:exampleTap];
    
    self.exampleTranslateLabel = [[UILabel alloc]initWithFrame:CGRectMake(30,CGRectGetMaxY(self.exampleLabel.frame)+10,self.width-30-20,20)];
    self.exampleTranslateLabel.numberOfLines = 0;
    self.exampleTranslateLabel.userInteractionEnabled = YES;
    self.exampleTranslateLabel.textColor = KFontGrayColor;
    self.exampleTranslateLabel.font = JPUIFontPingFangSCRegular(14);
    [self addSubview:self.exampleTranslateLabel];
    
    //小结
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(24,30,self.width-48,24)];
    self.titleLabel.font = JPUIFontPingFangBold(17);
    self.titleLabel.textColor = KFontBlackColor;
    self.titleLabel.text = @"本组单词小结";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:self.titleLabel];
    self.titleLabel.hidden = YES;
    
    self.beginBtn = [[UIButton alloc]initWithFrame:CGRectMake(24,self.height-100,self.width-48,40)];
    self.beginBtn.backgroundColor = KThemeColor;
    self.beginBtn.layer.cornerRadius = 20;
    self.beginBtn.layer.masksToBounds = YES;
    [self.beginBtn setTitle:@"开始测试" forState:UIControlStateNormal];
    [self.beginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.beginBtn.titleLabel.font = JPUIFontPingFangSCMedium(14);
    self.beginBtn.ym_acceptEventInterval = 2.0;
    [self.beginBtn addTarget:self action:@selector(beginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.beginBtn];
    self.beginBtn.hidden = YES;
    
    CGFloat t_h = self.height-100-68-32;
    CGFloat row_h = t_h/5.0;
    if(row_h < 50){
        row_h = 50;
    }
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(24,68,self.width-48,t_h) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = row_h;
    [self.tableView registerClass:NSClassFromString(@"WordSummaryListCell") forCellReuseIdentifier:@"WordSummaryListCell"];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.tableView];
    self.tableView.hidden = YES;
    
    //测试
    CGFloat h140 = [DeviceUtils getAdaptedHeightForDesignHeight:140 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(left,h140,2,67)];
    self.questionLabel.font = JPUIFontNotoSansJPMedium(48);
    self.questionLabel.textColor = KFontBlackColor;
    self.questionLabel.textAlignment = NSTextAlignmentCenter;
    self.questionLabel.userInteractionEnabled = YES;
    [self addSubview:self.questionLabel];
    self.questionLabel.hidden = YES;
    
    CGFloat w68 = [DeviceUtils getAdaptedWidthForDesignWidth:68 andCurrentAvailableWidth:self.currentAvailableWidth];
    CGFloat w76 = [DeviceUtils getAdaptedWidthForDesignWidth:76 andCurrentAvailableWidth:self.currentAvailableWidth];
    if(w76 > 100){
        w76 = 100;
    }
    CGFloat h58 = [DeviceUtils getAdaptedHeightForDesignHeight:58 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.errorBtn = [[UIButton alloc]initWithFrame:CGRectMake(w68,self.height-h58-w76,w76,w76)];
    [self.errorBtn setBackgroundImage:[UIImage imageNamed:@"card_error_logo"] forState:UIControlStateNormal];
    [self.errorBtn addTarget:self action:@selector(errorBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.errorBtn];
    self.errorBtn.hidden = YES;
    
    self.correctBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width-w76-w68,self.height-h58-w76,w76,w76)];
    [self.correctBtn setBackgroundImage:[UIImage imageNamed:@"card_correct_logo"] forState:UIControlStateNormal];
    [self.correctBtn addTarget:self action:@selector(correctBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.correctBtn];
    self.correctBtn.hidden = YES;
   
    CGFloat h118 = [DeviceUtils getAdaptedHeightForDesignHeight:118 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,self.height-h118-w76,self.width-32, 22)];
    self.tipLabel.font = JPUIFontPingFangSCRegular(16);
    self.tipLabel.textColor = KFontGrayColor;
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    self.tipLabel.text = @"是否知道读音和意思？";
    [self addSubview:self.tipLabel];
    self.tipLabel.hidden = YES;
    
    CGFloat w80 = [DeviceUtils getAdaptedWidthForDesignWidth:80 andCurrentAvailableWidth:self.currentAvailableWidth];
    if(w80 > 120){
        w80 = 120;
    }
    self.pronunciationImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.width-w80)/2.0,h140,w80,w80)];
    self.pronunciationImageView.image = [UIImage imageNamed:@"word_test_pronunciation_logo"];
    self.pronunciationImageView.userInteractionEnabled = YES;
    [self addSubview:self.pronunciationImageView];
    self.pronunciationImageView.hidden = YES;
    UITapGestureRecognizer *ptap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if(weakSelf.model.audio&&![weakSelf.model.audio isEqualToString:@""]){
            //有单词音频
            [weakSelf playAudio:weakSelf.model.audio];
        }else{
            if((weakSelf.model.pronunciation)&&(![weakSelf.model.pronunciation isEqualToString:@""])){
                if((weakSelf.model.writing)&&(weakSelf.model.isReadWriting)&&([weakSelf.model.isReadWriting isEqualToString:@"1"]||weakSelf.model.isReadWriting.integerValue == 1)){
                    [weakSelf speakJapaneseText:weakSelf.model.writing];
                }else{
                    [weakSelf speakJapaneseText:weakSelf.model.pronunciation];
                }
            }else{
                if((weakSelf.model.writing)&&(![weakSelf.model.writing isEqualToString:@""])){
                    [weakSelf speakJapaneseText:weakSelf.model.writing];
                }
            }
        }
    }];
    [self.pronunciationImageView addGestureRecognizer:ptap];
    
    //翻牌后
    self.knowBgView = [[UIView alloc]initWithFrame:CGRectMake((self.width-126)/2.0,self.height-94-36,126,36)];
    self.knowBgView.backgroundColor = [UIColor whiteColor];
    self.knowBgView.layer.cornerRadius = 18;
    self.knowBgView.layer.shadowColor = UIColorHex_(0x000000,0.06).CGColor;
    self.knowBgView.layer.shadowOffset = CGSizeMake(0,0);
    self.knowBgView.layer.shadowOpacity = 1;
    self.knowBgView.layer.shadowRadius = 6;
    [self addSubview:self.knowBgView];
    self.knowBgView.hidden = YES;
    
    self.unknowBtn = [[UIButton alloc]initWithFrame:CGRectMake(4,4,58,28)];
    [self.unknowBtn setTitle:@"不知道" forState:UIControlStateNormal];
    [self.unknowBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    [self.unknowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    self.unknowBtn.titleLabel.font = JPUIFontPingFangSCRegular(14);
    self.unknowBtn.layer.cornerRadius = 14;
    self.unknowBtn.layer.masksToBounds = YES;
    [self.unknowBtn addTarget:self action:@selector(unknowBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.knowBgView addSubview:self.unknowBtn];
    
    self.knownBtn = [[UIButton alloc]initWithFrame:CGRectMake(64,4,58,28)];
    [self.knownBtn setTitle:@"知道" forState:UIControlStateNormal];
    [self.knownBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    [self.knownBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    self.knownBtn.titleLabel.font = JPUIFontPingFangSCRegular(14);
    self.knownBtn.layer.cornerRadius = 14;
    self.knownBtn.layer.masksToBounds = YES;
    [self.knownBtn addTarget:self action:@selector(knownBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.knowBgView addSubview:self.knownBtn];
    
    self.nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(24,self.height-64,self.width-48,40)];
    [self.nextBtn setTitle:@"下一个" forState:UIControlStateNormal];
    [self.nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.nextBtn.titleLabel.font = JPUIFontPingFangSCMedium(14);
    self.nextBtn.backgroundColor = KThemeColor;
    self.nextBtn.layer.cornerRadius = 20;
    self.nextBtn.layer.masksToBounds = YES;
    self.nextBtn.ym_acceptEventInterval = 2.0;
    [self.nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.nextBtn];
    self.nextBtn.hidden = YES;
    
}

-(void)setType:(NSInteger)type{
    _type = type;
    CGFloat w = self.currentAvailableWidth-40;
    self.knowBgView.hidden = YES;
    self.nextBtn.hidden = YES;
    if(self.isTest == YES){
        self.writeLabel.hidden = YES;
        self.topErrorBtn.hidden = YES;
        self.collectBtn.hidden = YES;
        self.pronunciationLabel.hidden = YES;
        self.wordLabel.hidden = YES;
        self.exampleLabel.hidden = YES;
        self.exampleTranslateLabel.hidden = YES;
        self.toneLabel.hidden = YES;
        self.wordTipLabel.hidden = YES;
        self.exampleTipLabel.hidden = YES;
        self.titleLabel.hidden = YES;
        self.beginBtn.hidden = YES;
        self.tableView.hidden = YES;
        self.correctBtn.hidden = NO;
        self.errorBtn.hidden = NO;
        self.tipLabel.hidden = NO;
        if([_model.mode isEqualToString:@"2"]){
            self.pronunciationImageView.hidden = NO;
            self.questionLabel.hidden = YES;
        }else{
            self.pronunciationImageView.hidden = YES;
            self.questionLabel.hidden = NO;
        }
        self.questionLabel.font = JPUIFontNotoSansJPMedium(48);
        if([_model.mode isEqualToString:@"3"]){
            self.questionLabel.font = JPUIFontPingFangSCMedium(48);
        }
        CGFloat w2 = [self getStringWidthWithText:self.questionLabel.text font:self.questionLabel.font viewHeight:67];
        CGFloat h140 = [DeviceUtils getAdaptedHeightForDesignHeight:140 andCurrentAvailableHeight:self.currentAvailableHeight];
        CGFloat h58 = [DeviceUtils getAdaptedHeightForDesignHeight:58 andCurrentAvailableHeight:self.currentAvailableHeight];
        CGFloat h118 = [DeviceUtils getAdaptedHeightForDesignHeight:118 andCurrentAvailableHeight:self.currentAvailableHeight];
        CGFloat w76 = [DeviceUtils getAdaptedWidthForDesignWidth:76 andCurrentAvailableWidth:self.currentAvailableWidth];
        CGFloat w68 = [DeviceUtils getAdaptedWidthForDesignWidth:68 andCurrentAvailableWidth:self.currentAvailableWidth];
        if(w76 > 100){
            w76 = 100;
        }
        CGFloat w80 = [DeviceUtils getAdaptedWidthForDesignWidth:80 andCurrentAvailableWidth:self.currentAvailableWidth];
        if(w80 > 120){
            w80 = 120;
        }
        if(type == 1){
            self.bgView.hidden = NO;
            if(w2 > (w-34)){
                w2 = w-34;
                self.questionLabel.numberOfLines = 0;
                if([_model.mode isEqualToString:@"3"]){
                    self.questionLabel.font = JPUIFontPingFangSCMedium(30);
                }else{
                    self.questionLabel.font = JPUIFontNotoSansJPMedium(30);
                }
                CGFloat left = (w-w2-2)/2.0;
                CGFloat q_h = [self getStringHeightWithText:self.questionLabel.text font:self.questionLabel.font viewWidth:w2+2];
                self.questionLabel.frame = CGRectMake(left,h140,w2+2,q_h+2);
            }else{
                CGFloat left = (w-w2-2)/2.0;
                self.questionLabel.numberOfLines = 1;
                if([_model.mode isEqualToString:@"3"]){
                    self.questionLabel.font = JPUIFontPingFangSCMedium(48);
                }else{
                    self.questionLabel.font = JPUIFontNotoSansJPMedium(48);
                }
                self.questionLabel.frame = CGRectMake(left,h140,w2+2,67);
            }
            self.correctBtn.frame = CGRectMake(w-w76-w68,self.height-h58-w76,w76,w76);
            self.tipLabel.frame = CGRectMake(16,self.height-h118-w76,w-32, 22);
            self.pronunciationImageView.frame = CGRectMake((w-w80)/2.0, h140,w80,w80);
        }else if (type == 2){
            self.bgView.hidden = YES;
            if(w2 > (w-34-24)){
                w2 = w-34-24;
                self.questionLabel.numberOfLines = 0;
                if([_model.mode isEqualToString:@"3"]){
                    self.questionLabel.font = JPUIFontPingFangSCMedium(30);
                }else{
                    self.questionLabel.font = JPUIFontNotoSansJPMedium(30);
                }
                CGFloat left = (w-w2-2)/2.0;
                CGFloat q_h = [self getStringHeightWithText:self.questionLabel.text font:self.questionLabel.font viewWidth:w2+2];
                self.questionLabel.frame = CGRectMake(left,h140,w2+2,q_h+2);
            }else{
                CGFloat left = (w-w2-2)/2.0;
                self.questionLabel.numberOfLines = 1;
                if([_model.mode isEqualToString:@"3"]){
                    self.questionLabel.font = JPUIFontPingFangSCMedium(48);
                }else{
                    self.questionLabel.font = JPUIFontNotoSansJPMedium(48);
                }
                self.questionLabel.frame = CGRectMake(left,h140,w2+2,67);
            }
            self.correctBtn.frame = CGRectMake(w-24-w76-w68,self.height-h58-w76,w76,w76);
            self.tipLabel.frame = CGRectMake(16,self.height-h118-w76,w-24-32,22);
            self.pronunciationImageView.frame = CGRectMake((w-24-w80)/2.0,h140,w80,w80);

        }else{
            self.bgView.hidden = YES;
            if(w2 > (w-34-48)){
                w2 = w-34-48;
                self.questionLabel.numberOfLines = 0;
                if([_model.mode isEqualToString:@"3"]){
                    self.questionLabel.font = JPUIFontPingFangSCMedium(30);
                }else{
                    self.questionLabel.font = JPUIFontNotoSansJPMedium(30);
                }
                CGFloat left = (w-w2-2)/2.0;
                CGFloat q_h = [self getStringHeightWithText:self.questionLabel.text font:self.questionLabel.font viewWidth:w2+2];
                self.questionLabel.frame = CGRectMake(left,h140,w2+2,q_h+2);
            }else{
                CGFloat left = (w-w2-2)/2.0;
                self.questionLabel.numberOfLines = 1;
                if([_model.mode isEqualToString:@"3"]){
                    self.questionLabel.font = JPUIFontPingFangSCMedium(48);
                }else{
                    self.questionLabel.font = JPUIFontNotoSansJPMedium(48);
                }
                self.questionLabel.frame = CGRectMake(left,h140,w2+2,67);
            }
            self.correctBtn.frame = CGRectMake(w-48-w76-w68,self.height-h58-w76,w76,w76);
            self.tipLabel.frame = CGRectMake(16,self.height-h118-w76,w-48-32, 22);
            self.pronunciationImageView.frame = CGRectMake((w-48-w80)/2.0,h140,w80,w80);

        }
       
    }else{
        self.questionLabel.hidden = YES;
        self.correctBtn.hidden = YES;
        self.errorBtn.hidden = YES;
        self.tipLabel.hidden = YES;
        self.pronunciationImageView.hidden = YES;
        CGFloat h100 = [DeviceUtils getAdaptedHeightForDesignHeight:100 andCurrentAvailableHeight:self.currentAvailableHeight];
        CGFloat h120 = [DeviceUtils getAdaptedHeightForDesignHeight:120 andCurrentAvailableHeight:self.currentAvailableHeight];
        CGFloat h10 = [DeviceUtils getAdaptedHeightForDesignHeight:10 andCurrentAvailableHeight:self.currentAvailableHeight];
        CGFloat h25 = [DeviceUtils getAdaptedHeightForDesignHeight:25 andCurrentAvailableHeight:self.currentAvailableHeight];
        if(self.isLast == YES){
            self.topErrorBtn.hidden = YES;
            self.collectBtn.hidden = YES;
            self.pronunciationLabel.hidden = YES;
            self.writeLabel.hidden = YES;
            self.wordLabel.hidden = YES;
            self.exampleLabel.hidden = YES;
            self.exampleTranslateLabel.hidden = YES;
            self.toneLabel.hidden = YES;
            self.wordTipLabel.hidden = YES;
            self.exampleTipLabel.hidden = YES;
            self.titleLabel.hidden = NO;
            self.beginBtn.hidden = NO;
            self.tableView.hidden = NO;
            CGFloat t_h = self.height-100-68-32;
            CGFloat row_h = t_h/5.0;
            if(row_h < 50){
                row_h = 50;
            }
            if(type == 1){
                self.bgView.hidden = NO;
                self.titleLabel.frame = CGRectMake(24,30,w-48,24);
                self.beginBtn.frame = CGRectMake(24,self.height-100,w-48,40);
                self.tableView.frame = CGRectMake(24,68,w-48,t_h);
            }else if (type == 2){
                self.bgView.hidden = YES;
                self.titleLabel.frame = CGRectMake(24,30,w-48-24,24);
                self.beginBtn.frame = CGRectMake(24,self.height-100,w-48-24,40);
                self.tableView.frame = CGRectMake(24,68,w-24-48,t_h);
            }else{
                self.bgView.hidden = YES;
                self.titleLabel.frame = CGRectMake(24,30,w-48-48,24);
                self.beginBtn.frame = CGRectMake(24,self.height-100,w-48-48,40);
                self.tableView.frame = CGRectMake(24,68,w-48-48,t_h);
            }
        }else{
            self.topErrorBtn.hidden = NO;
            self.collectBtn.hidden = NO;
            self.pronunciationLabel.hidden = NO;
            self.writeLabel.hidden = NO;
            self.wordLabel.hidden = NO;
            self.exampleLabel.hidden = NO;
            self.exampleTranslateLabel.hidden = NO;
            self.toneLabel.hidden = NO;
            if(_model.speech&&(![_model.speech isEqualToString:@""])){
                self.wordTipLabel.hidden = NO;
            }else{
                self.wordTipLabel.hidden = YES;
            }
            self.exampleTipLabel.hidden = NO;
            self.titleLabel.hidden = YES;
            self.beginBtn.hidden = YES;
            self.tableView.hidden = YES;
            CGFloat w2 = [self getStringWidthWithText:self.writeLabel.text font:self.writeLabel.font viewHeight:67];
            CGFloat tone_w = [self getStringWidthWithText:self.toneLabel.text font:self.toneLabel.font viewHeight:28];
            if(type == 1){
                self.bgView.hidden = NO;
                self.collectBtn.frame = CGRectMake(w-46,6,40,40);
                self.pronunciationLabel.frame = CGRectMake(16,h120,w-32,34);
                if(w2 > (w-34-tone_w*2-20)){
                    w2 = w-34-tone_w*2-20;
                    self.writeLabel.adjustsFontSizeToFitWidth = YES;
                }else{
                    self.writeLabel.adjustsFontSizeToFitWidth = NO;
                }
                CGFloat left = (w-w2-2)/2.0;
                self.writeLabel.frame = CGRectMake(left,CGRectGetMaxY(self.pronunciationLabel.frame),w2+2,67);
                CGFloat w_tip_w = [self getStringWidthWithText:self.wordTipLabel.text font:self.wordTipLabel.font viewHeight:22];
                self.wordTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.writeLabel.frame)+h100,w_tip_w+20,22);
                CGSize w_size = [self.wordLabel sizeThatFits:CGSizeMake(w-30-20,MAXFLOAT)];
                self.wordLabel.frame = CGRectMake(30,CGRectGetMaxY(self.wordTipLabel.frame)+h10,w-30-20,ceilf(w_size.height));
                self.exampleTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.wordLabel.frame)+h25,46,22);
                CGSize e_size = [self.exampleLabel sizeThatFits:CGSizeMake(w-30-20,MAXFLOAT)];
                self.exampleLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleTipLabel.frame)+h10,w-30-20,ceilf(e_size.height));
                CGSize e_size2 = [self.exampleTranslateLabel sizeThatFits:CGSizeMake(w-30-20,MAXFLOAT)];
                self.exampleTranslateLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleLabel.frame)+10,w-30-20,ceilf(e_size2.height));
            }else{
                self.bgView.hidden = YES;
                if(type == 2){
                    self.collectBtn.frame = CGRectMake(w-24-46,6,40,40);
                    self.pronunciationLabel.frame = CGRectMake(16,h120,w-32-24,34);
                    if(w2 > (w-34-24-tone_w*2-20)){
                        w2 = w-34-24-tone_w*2-20;
                        self.writeLabel.adjustsFontSizeToFitWidth = YES;
                    }else{
                        self.writeLabel.adjustsFontSizeToFitWidth = NO;
                    }
                    CGFloat left = (w-w2-2)/2.0;
                    self.writeLabel.frame = CGRectMake(left,CGRectGetMaxY(self.pronunciationLabel.frame),w2+2,67);
                    CGFloat w_tip_w = [self getStringWidthWithText:self.wordTipLabel.text font:self.wordTipLabel.font viewHeight:22];
                    self.wordTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.writeLabel.frame)+h100,w_tip_w+20,22);
                    CGSize w_size = [self.wordLabel sizeThatFits:CGSizeMake(w-30-20-24,MAXFLOAT)];
                    self.wordLabel.frame = CGRectMake(30,CGRectGetMaxY(self.wordTipLabel.frame)+h10,w-30-20-24,ceilf(w_size.height));
                    self.exampleTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.wordLabel.frame)+h25,46,22);
                    CGSize e_size = [self.exampleLabel sizeThatFits:CGSizeMake(w-30-20-24,MAXFLOAT)];
                    self.exampleLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleTipLabel.frame)+h10,w-30-20-24,ceilf(e_size.height));
                    CGSize e_size2 = [self.exampleTranslateLabel sizeThatFits:CGSizeMake(w-30-20-24,MAXFLOAT)];
                    self.exampleTranslateLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleLabel.frame)+10,w-30-20-24,ceilf(e_size2.height));
                }else{
                    self.collectBtn.frame = CGRectMake(w-48-46,6,40,40);
                    self.pronunciationLabel.frame = CGRectMake(16,h120,w-32-48,34);
                    if(w2 > (w-34-48-tone_w*2-20)){
                        w2 = w-34-48-tone_w*2-20;
                        self.writeLabel.adjustsFontSizeToFitWidth = YES;
                    }else{
                        self.writeLabel.adjustsFontSizeToFitWidth = NO;
                    }
                    CGFloat left = (w-w2-2)/2.0;
                    self.writeLabel.frame = CGRectMake(left,CGRectGetMaxY(self.pronunciationLabel.frame),w2+2,67);
                    CGFloat w_tip_w = [self getStringWidthWithText:self.wordTipLabel.text font:self.wordTipLabel.font viewHeight:22];
                    self.wordTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.writeLabel.frame)+h100,w_tip_w+20,22);
                    CGSize w_size = [self.wordLabel sizeThatFits:CGSizeMake(w-30-20-48,MAXFLOAT)];
                    self.wordLabel.frame = CGRectMake(30,CGRectGetMaxY(self.wordTipLabel.frame)+h10,w-30-20-48,ceilf(w_size.height));
                    self.exampleTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.wordLabel.frame)+h25,46,22);
                    CGSize e_size = [self.exampleLabel sizeThatFits:CGSizeMake(w-30-20-48,MAXFLOAT)];
                    self.exampleLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleTipLabel.frame)+h10,w-30-20-48,ceilf(e_size.height));
                    CGSize e_size2 = [self.exampleTranslateLabel sizeThatFits:CGSizeMake(w-30-20-48,MAXFLOAT)];
                    self.exampleTranslateLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleLabel.frame)+10,w-30-20-48,ceilf(e_size2.height));
                }
            }
            self.toneLabel.frame = CGRectMake(CGRectGetMaxX(self.writeLabel.frame)+10, CGRectGetMaxY(self.writeLabel.frame)-self.writeLabel.frame.size.height+6,tone_w,28);
        }
    }
}


-(void)setModel:(WordListModel *)model{
    _model = model;
    self.pronunciationLabel.text = model.pronunciation;
    self.writeLabel.text = model.writing;
    self.toneLabel.text = model.tone;
    self.wordLabel.text = model.meaning;
    self.wordTipLabel.text = model.speech;
    self.exampleLabel.text = model.example;
    self.exampleTranslateLabel.text = model.exampleMeaning;
    if([model.isCollect isEqualToString:@"1"]){
        self.collectBtn.selected = YES;
    }else{
        self.collectBtn.selected = NO;
    }
    if(self.isTest == YES){
        if([model.mode isEqualToString:@"1"]){
            self.tipLabel.text = @"是否知道读音和意思？";
            self.questionLabel.text = model.writing;
        }else if ([model.mode isEqualToString:@"2"]){
            self.tipLabel.text = @"是否知道书写和意思？";
            self.questionLabel.text = model.writing;
        }else if ([model.mode isEqualToString:@"3"]){
            self.tipLabel.text = @"是否知道书写和读音？";
            self.questionLabel.text = model.meaning;
        }
    }
}

-(void)setWordArr:(NSArray *)wordArr{
    _wordArr = wordArr;
    self.playIndex = -1;
    [self.tableView reloadData];
}


#pragma mark - 开始测试
-(void)beginBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(beginTestTap:)]){
        [self.delegate beginTestTap:self];
    }
}

#pragma mark - 报错
-(void)topErrorBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(applyErrorTap:)]){
        [self.delegate applyErrorTap:self];
    }
}

#pragma mark - 收藏
-(void)collectBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectTap:andIsCollect:)]){
        [self.delegate collectTap:self andIsCollect:self.collectBtn.selected];
    }
}

#pragma mark - 测试选了错误
-(void)errorBtnClick{
    self.unknowBtn.selected = YES;
    self.knownBtn.selected = NO;
    self.knownBtn.backgroundColor = [UIColor whiteColor];
    self.unknowBtn.backgroundColor = UIColorHex(0xFE4A4B);
    [self showCardAnimation];
}

#pragma mark - 测试选了正确
-(void)correctBtnClick{
    self.unknowBtn.selected = NO;
    self.knownBtn.selected = YES;
    self.unknowBtn.backgroundColor = [UIColor whiteColor];
    self.knownBtn.backgroundColor = KThemeColor;
    [self showCardAnimation];
}

#pragma mark - 下一个
-(void)nextBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(nextTap:andIsKnow:)]){
        [self.delegate nextTap:self andIsKnow:self.knownBtn.selected];
    }
}

#pragma mark - 不知道
-(void)unknowBtnClick{
    self.unknowBtn.selected = YES;
    self.knownBtn.selected = NO;
    self.knownBtn.backgroundColor = [UIColor whiteColor];
    self.unknowBtn.backgroundColor = UIColorHex(0xFE4A4B);
}

#pragma mark - 知道
-(void)knownBtnClick{
    self.unknowBtn.selected = NO;
    self.knownBtn.selected = YES;
    self.unknowBtn.backgroundColor = [UIColor whiteColor];
    self.knownBtn.backgroundColor = KThemeColor;
}


#pragma mark - 翻牌后界面
-(void)showOppositeView{
    self.correctBtn.hidden = YES;
    self.errorBtn.hidden = YES;
    self.tipLabel.hidden = YES;
    self.pronunciationImageView.hidden = YES;
    self.questionLabel.hidden = YES;
    self.titleLabel.hidden = YES;
    self.beginBtn.hidden = YES;
    self.tableView.hidden = YES;
    self.topErrorBtn.hidden = NO;
    self.collectBtn.hidden = NO;
    self.pronunciationLabel.hidden = NO;
    self.writeLabel.hidden = NO;
    self.wordLabel.hidden = NO;
    self.exampleLabel.hidden = NO;
    self.exampleTranslateLabel.hidden = NO;
    self.toneLabel.hidden = NO;
    if(_model.speech&&(![_model.speech isEqualToString:@""])){
        self.wordTipLabel.hidden = NO;
    }else{
        self.wordTipLabel.hidden = YES;
    }
    self.exampleTipLabel.hidden = NO;
    self.knowBgView.hidden = NO;
    self.nextBtn.hidden = NO;
    CGFloat w = self.currentAvailableWidth-40;
    CGFloat w2 = [self getStringWidthWithText:self.writeLabel.text font:self.writeLabel.font viewHeight:67];
    CGFloat tone_w = [self getStringWidthWithText:self.toneLabel.text font:self.toneLabel.font viewHeight:28];
    self.collectBtn.frame = CGRectMake(w-46,6,40,40);
    CGFloat h120 = [DeviceUtils getAdaptedHeightForDesignHeight:120 andCurrentAvailableHeight:self.currentAvailableHeight];
    CGFloat h100 = [DeviceUtils getAdaptedHeightForDesignHeight:100 andCurrentAvailableHeight:self.currentAvailableHeight];
    CGFloat h10 = [DeviceUtils getAdaptedHeightForDesignHeight:10 andCurrentAvailableHeight:self.currentAvailableHeight];
    CGFloat h25 = [DeviceUtils getAdaptedHeightForDesignHeight:25 andCurrentAvailableHeight:self.currentAvailableHeight];
    self.pronunciationLabel.frame = CGRectMake(16,h120,w-32,34);
    if(w2 > (w-34-tone_w*2-20)){
        w2 = w-34-tone_w*2-20;
        self.writeLabel.adjustsFontSizeToFitWidth = YES;
    }else{
        self.writeLabel.adjustsFontSizeToFitWidth = NO;
    }
    CGFloat left = (w-w2-2)/2.0;
    self.writeLabel.frame = CGRectMake(left,CGRectGetMaxY(self.pronunciationLabel.frame),w2+2,67);
    CGFloat w_tip_w = [self getStringWidthWithText:self.wordTipLabel.text font:self.wordTipLabel.font viewHeight:22];
    self.wordTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.writeLabel.frame)+h100,w_tip_w+20,22);
    CGSize w_size = [self.wordLabel sizeThatFits:CGSizeMake(w-30-20, MAXFLOAT)];
    self.wordLabel.frame = CGRectMake(30,CGRectGetMaxY(self.wordTipLabel.frame)+h10,w-30-20,ceilf(w_size.height));
    self.exampleTipLabel.frame = CGRectMake(20,CGRectGetMaxY(self.wordLabel.frame)+h25,46,22);
    CGSize e_size = [self.exampleLabel sizeThatFits:CGSizeMake(w-30-20, MAXFLOAT)];
    self.exampleLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleTipLabel.frame)+h10,w-30-20,ceilf(e_size.height));
    CGSize e_size2 = [self.exampleTranslateLabel sizeThatFits:CGSizeMake(w-30-20, MAXFLOAT)];
    self.exampleTranslateLabel.frame = CGRectMake(30,CGRectGetMaxY(self.exampleLabel.frame)+10,w-30-20,ceilf(e_size2.height));
    self.toneLabel.frame = CGRectMake(CGRectGetMaxX(self.writeLabel.frame)+10, CGRectGetMaxY(self.writeLabel.frame)-self.writeLabel.frame.size.height+6,tone_w,28);
}

#pragma mark - 翻牌效果
-(void)showCardAnimation{
    __weak typeof(self)self_weak_ = self;
    [UIView transitionWithView:self duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        self_weak_.bgView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
    
    [self showOppositeView];

}

#pragma mark - tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WordSummaryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WordSummaryListCell"];
    if(indexPath.row < self.wordArr.count){
        WordListModel *model = self.wordArr[indexPath.row];
        cell.wordModel = model;
        __weak typeof(self)self_weak_ = self;
        __weak typeof(cell)cell_weak_ = cell;
        [cell setPlayBlock:^{
            //停止正在播放的
            if(self_weak_.playIndex == indexPath.row){
                //暂停
                self_weak_.playIndex = -1;
                [cell_weak_ stopVoiceAnimation];
                [self_weak_ stopSpeaking];
                self_weak_.currentCell = cell_weak_;
            }else{
                //播放
                [self_weak_ stopSpeaking];
                self_weak_.currentCell = cell_weak_;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    self_weak_.playIndex = indexPath.row;
                    [cell_weak_ startVoiceAnimation];
                    if(model.audio&&![model.audio isEqualToString:@""]){
                        //有单词音频
                        [self_weak_ playAudio:model.audio];
                    }else{
                        if((model.pronunciation)&&(![model.pronunciation isEqualToString:@""])){
                            if((model.writing)&&(model.isReadWriting)&&([model.isReadWriting isEqualToString:@"1"]||model.isReadWriting.integerValue == 1)){
                                [self_weak_ speakJapaneseText:model.writing];
                            }else{
                                [self_weak_ speakJapaneseText:model.pronunciation];
                            }
                        }else{
                            if((model.writing)&&(![model.writing isEqualToString:@""])){
                                [self_weak_ speakJapaneseText:model.writing];
                            }
                        }
                    }
                });
            }
        }];
        
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.wordArr.count;
}

#pragma mark - 播放读取文本
- (void)speakJapaneseText:(NSString *)text{
    
    [self listentTypeSet];
    
//    if(!self.synthesizer){
//        self.synthesizer = [[AVSpeechSynthesizer alloc] init];
//        self.synthesizer.delegate = self;
//    }
//    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
//    utterance.rate = 0.53; // 语速，范围0.0~1.0
//    utterance.pitchMultiplier = 1.1; // 音高，范围0.5~2.0
//    utterance.volume = 1.0; // 音量，范围0~1
//    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"ja-JP"];
//    utterance.voice = voice;
//    [self.synthesizer speakUtterance:utterance];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(speakText: andText:)]){
        [self.delegate speakText:self andText:text];
    }

}

#pragma mark - 停止当前正在读的文本
- (void)stopSpeaking{
    
//    if (self.synthesizer&&self.synthesizer.isSpeaking) {
//        [self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
//        if(self.currentCell){
//            [self.currentCell stopVoiceAnimation];
//        }
//    }
    if(self.currentCell){
        [self.currentCell stopVoiceAnimation];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(stopSpeak:)]){
        [self.delegate stopSpeak:self];
    }
    
}

#pragma mark - 直接播放音频
-(void)playAudio:(NSString*)audio{
    [self listentTypeSet];
    if (self.delegate && [self.delegate respondsToSelector:@selector(playAudio:andAudio:)]){
        [self.delegate playAudio:self andAudio:audio];
    }
}



//#pragma mark - AVSpeechSynthesizer deletage
//- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
//    // 当前文本已经读完
//    if(self.currentCell){
//        [self.currentCell stopVoiceAnimation];
//    }
//    self.playIndex =  -1;
//}

#pragma mark - 停止播放动画
-(void)stopPlayAnimation{
    if(self.currentCell){
        [self.currentCell stopVoiceAnimation];
        self.playIndex = -1;
    }
}

#pragma mark - 设置静音模式下也能播放音频
-(void)listentTypeSet{
    // 获取当前的音频会话类别
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    
    // 检查当前类别是否已经是 AVAudioSessionCategoryPlayback
    if (![audioSession.category isEqualToString:AVAudioSessionCategoryPlayback]) {
        NSError *setCategoryError = nil;
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
        
        if (setCategoryError) {
            NSLog(@"Error setting category: %@", [setCategoryError localizedDescription]);
        }
    }
    
    // 检查音频会话是否已经处于激活状态
    if (!audioSession.isOtherAudioPlaying) {
        NSError *activationError = nil;
        [audioSession setActive:YES error:&activationError];
        
        if (activationError) {
            NSLog(@"Error activating session: %@", [activationError localizedDescription]);
        }
    }
}




@end
